<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    body{
      height:2000px;
      background:red;
    }
  </style>
</head>
<body>
  
  <div>
  </div>
  <script>

    const throttle = (fn, wait=50)=>{
      let lastTime = 0
      return function(...args){
        // 返回节流的函数
        let now = new Date()
        if(now-lastTime>wait){
          // 喝上次执行的时间差，大于指定的间隔
          lastTime = now
          fn.apply(this, args)
        }
      }
    }


    const debounce = (fn, wait=500)=>{
      let timer
      return function(...args){
        if(timer) clearTimeout(timer)
        timer = setTimeout(()=>{
          fn.apply(this, args)
        }, wait)
      }
    }

    // const obj = {
    //   name:'开课吧',
    //   sayHi: debounce(function(){
    //     console.log(this)
    //   })

    // }
    // obj.sayHi()
    // const sayHi = debounce(function(){
    //     console.log(this)
    //   })
    // sayHi()
    // setInterval(
    //   throttle(()=>{
    //     console.log('节流后')
    //   },1000),
    //   50
    // )
    let obj1 = {
      name:'开课吧'
    }
    let obj2 = {
      name:'前端性能优化'
    }
    function sayHi(wor1, word2){
      console.log(this.name, wor1, word2)
    }
    sayHi(1,2)
    sayHi.apply(obj1, [1,2])
    sayHi.call(obj2, 1, 2)
    // window.addEventListener('scroll', throttle(function(){
    //   console.log('滚动监听， 图片懒加载')
    // }, 2000)

    // )
    window.addEventListener('scroll', debounce(()=>{
      console.log('滚动监听， 图片懒加载')
    }, 2000)

    )
  </script>
</body>
</html>